//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.变长滑动窗口.越长约合法;

import java.util.HashMap;
import java.util.HashSet;

public class demo_2799 {
    public demo_2799() {
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1786, 1786, 1786, 114};
        System.out.println(countCompleteSubarrays(nums));
    }

    public static int countCompleteSubarrays(int[] nums) {
        HashSet<Integer> set = new HashSet();
        int n = nums.length;
        int max = Integer.MIN_VALUE;

        for(int num : nums) {
            set.add(num);
        }

        int k = set.size();
        int count = 0;

        for(int j = k; j < n; ++j) {
            HashMap<Integer, Integer> map = new HashMap();
            int left = 0;

            for(int i = 0; i < n; ++i) {
                int num = nums[i];
                map.put(num, (Integer)map.getOrDefault(num, 0) + 1);
                if ((Integer)map.get(num) == 0) {
                    map.remove(num);
                }

                if (i >= j - 1) {
                    if (map.size() == k) {
                        ++count;
                    }

                    int out = nums[left++];
                    map.put(out, (Integer)map.getOrDefault(out, 0) - 1);
                    if ((Integer)map.get(out) == 0) {
                        map.remove(out);
                    }
                }
            }
        }

        return count;
    }
}
